﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using System.ServiceModel.Syndication;
using System.IO;
using SBLib.RSS.Model;

namespace SBLib.RSS
{
    public class RssReader
    {
        public ModelRssInfo ReadRssFeed(string Feedpath)
        {
            try
            {
                HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(Feedpath);

                //// Create the web request based on the URL provided for the feed. 
                //WebRequest req = WebRequest.Create(Feedpath);

                //// Get the data from the feed. 
                //WebResponse res = req.GetResponse();

                // Simple test for the type of feed: Atom 1.0 or RSS 2.0. 
                SyndicationFeedFormatter formatter = null;
                XElement feed = XElement.Load(new StreamReader(rssFeed.GetResponse().GetResponseStream()));
                
                if (feed.Name.LocalName == "rss")
                {
                    formatter = new Rss20FeedFormatter();
                }
                else if (feed.Name.LocalName == "feed")
                {
                    formatter = new Atom10FeedFormatter();
                }

                // Read the feed data into the formatter. 
                formatter.ReadFrom(feed.CreateReader());

                ModelRssInfo rssInfo = new ModelRssInfo();
                rssInfo.Title = formatter.Feed.Title.Text;
                rssInfo.Description = formatter.Feed.Description.Text;
                rssInfo.Items = new List<ModelRssItem>();


                //items
                // Display the item data. 
                foreach (var item in formatter.Feed.Items)
                {
                    ModelRssItem _item = new ModelRssItem();

                    try
                    {
                        _item.Id = item.Id.ToString();
                    }
                    catch (Exception)
                    {

                        _item.Id = "";
                    }


                    try
                    {
                        _item.Title = item.Title.Text;
                    }
                    catch (Exception)
                    {

                        _item.Title = "";
                    }

                    try
                    {
                        _item.Summary = item.Summary.Text;
                    }
                    catch (Exception)
                    {

                        _item.Summary = "";
                    }

                    try
                    {
                        _item.Link = item.Links.FirstOrDefault().GetAbsoluteUri() != null ? item.Links.FirstOrDefault().GetAbsoluteUri().ToString() : "";
                    }
                    catch (Exception)
                    {

                        _item.Link="";
                    }

                    try
                    {
                        _item.PubDate = item.PublishDate.LocalDateTime.ToString("dd/MM/yyyy");
                    }
                    catch (Exception)
                    {

                        _item.PubDate = "";
                    }

                    try
                    {
                        _item.Author = item.Authors.FirstOrDefault() != null ? item.Authors.FirstOrDefault().ToString() : "";
                    }
                    catch (Exception)
                    {

                        _item.Author = "";
                    }
                    rssInfo.Items.Add(_item);
                    
                }

                return rssInfo;
            }
            catch (Exception)
            {

                return null;
            }
            return null;
        
        }
    }
}
